﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace myServer
{
    public class CommandShell
    {
        public string CurrentDirectory = "\\";
        public string CurrentDrive = "C:\\";

        public string Owner = "";

        public CommandShell(string owner)
        {
            Owner = owner;
        }

        public string GetDriveList(bool needType)
        {
            string str = "";
            DriveInfo[] info = System.IO.DriveInfo.GetDrives();

            for (int i = 0; i < info.Length; i++)
            {
                str += info[i].Name;
                if (needType)
                    str += " (" + info[i].DriveType + ")" + "\n";
                else
                    str += "\n";
            }

            return str;
        }

        public string GetDirList(string path)
        {
            string str = "";
            string[] strs = null;

            if (path == null || path.Equals(""))
                strs = System.IO.Directory.GetDirectories(CurrentDrive + CurrentDirectory);
            else
                strs = System.IO.Directory.GetDirectories(path);

            for (int i = 0; i < strs.Length; i++)
            {
                str +=  strs[i] + "\n";
            }

            return str;
        }

        public string GetFileList(string path)
        {
            string str = "";
            string[] strs = null;

            if (path == null || path.Equals(""))
                strs = System.IO.Directory.GetFiles(CurrentDrive + CurrentDirectory);
            else
                strs = System.IO.Directory.GetFiles(path);

            for (int i = 0; i < strs.Length; i++)
            {
                str += strs[i] + "\n";
            }

            return str;
        }

        //
        //  Structure:
        //      VolumeLabel
        //      DriveType
        //      DriveFormat
        //      AvailableFreeSpace
        //      TotalFreeSpace
        //      TotalSize
        //      IsReady
        //
        public string GetDriveInfo(string drv)
        {
            

            if (drv != null && !drv.Equals(""))
            {
                try
                {
                    string str = "";

                    DriveInfo info = new DriveInfo(drv);

                    str += info.VolumeLabel;
                    str += info.DriveType;
                    str += info.DriveFormat;
                    str += info.AvailableFreeSpace;
                    str += info.TotalFreeSpace;
                    str += info.TotalSize;
                    str += info.IsReady;
                }
                catch (Exception)
                {
                    return "";
                }
            }

            return "";
        }

        //
        //  Structure:
        //      CreateTime
        //      LastModifyTime
        //      LastAccessTime
        //      Attributes
        //
        public string GetDirInfo(string path)
        {
            if (path != null && !path.Equals(""))
            {
                try
                {
                    if (Directory.Exists(path))
                    {
                        string str = "";

                        DirectoryInfo info = new DirectoryInfo(path);

                        str += info.CreationTime;
                        str += info.LastWriteTime;
                        str += info.LastAccessTime;
                        str += info.Attributes;

                        return str;
                    }
                    else
                        return "";
                }
                catch (Exception)
                {
                    return "";
                }
            }
            return "";
        }

        //
        //  Structure:
        //      Size 
        //      CreateTime
        //      LastModifyTime
        //      LastAccessTime
        //      Attributes
        //
        public string GetFileInfo(string path)
        {
            if (path != null && !path.Equals("") )
            {
                try
                {
                    if (File.Exists(path))
                    {
                        string str = "";

                        FileInfo info = new FileInfo(path);

                        str += info.Length + "\n";
                        str += info.CreationTime + "\n";
                        str += info.LastWriteTime + "\n";
                        str += info.LastAccessTime + "\n";
                        str += info.Attributes + "\n";

                        return str;
                    }
                    else
                        return "";
                }
                catch (Exception)
                {
                    return "";
                }
            }
            return "";
        }

        public bool ChangeDrive(string name)
        {
            CurrentDrive = name;
            return true;
        }

        public bool ChangeDir(string dirname)
        {
            CurrentDirectory = dirname;
            return true;
        }

        public string GetDrive()
        {
            return CurrentDrive;
        }

        public string GetDir()
        {
            return CurrentDirectory;
        }

        public bool ExecuteCommand(string cmd)
        {
            return true;
        }
    }
}
